<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\DiscountXMedicine
 *
 * @property int $id
 * @property int $merchant_medicine_id 商品id
 * @property int $merchant_id 店铺id
 * @property int $discount_sales_rule_id 折扣id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DiscountXMedicine newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DiscountXMedicine newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\DiscountXMedicine onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DiscountXMedicine query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DiscountXMedicine whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DiscountXMedicine whereDiscountSalesRuleId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DiscountXMedicine whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DiscountXMedicine whereMerchantId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DiscountXMedicine whereMerchantMedicineId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DiscountXMedicine whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\DiscountXMedicine withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\DiscountXMedicine withoutTrashed()
 * @mixin \Eloquent
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DiscountXMedicine whereDeletedAt($value)
 */
class DiscountXMedicine extends Model
{
    protected $table='discount_x_medicine';
    use SoftDeletes;

    /**
     * 获取商品的满减优惠金额
     * @param $merchant_medicine_id
     * @param float $total_amount
     * @return float|int
     */
    public static function getGoodsReducePrice($merchant_medicine_id,$total_amount=0.01){
        $goods_rules=self::whereMerchantMedicineId($merchant_medicine_id)->pluck('discount_sales_rule_id')->toArray();
        if (count($goods_rules) > 0){
            $freeRules=DiscountSalesRule::whereType('reduce')->whereIn('id',$goods_rules)->whereStatus(1)->orderBy('satisfy_amount','desc')->get();
            if (count($freeRules) > 0){
                /** @var DiscountSalesRule $rule */
                foreach ($freeRules as $rule){
                    if (!is_null($rule->start_time) && !is_null($rule->end_time)){
                        if (strtotime($rule->start_time) <= time() && time() < strtotime($rule->end_time)){
                            if ($total_amount >= $rule->satisfy_amount){
                                return $rule->reduce_amount;
                            }
                        }
                    } else {
                        if ($total_amount >= $rule->satisfy_amount){
                            return $rule->reduce_amount;
                        }
                    }
                }
            }
        }
        return 0;
    }

    public static function getGoodsReduceName($merchant_medicine_id,$total_amount=0.01){
        $goods_rules=self::whereMerchantMedicineId($merchant_medicine_id)->pluck('discount_sales_rule_id')->toArray();
        if (count($goods_rules) > 0){
            $freeRules=DiscountSalesRule::whereType('reduce')->whereIn('id',$goods_rules)->whereStatus(1)->orderBy('satisfy_amount','desc')->get();
            if (count($freeRules) > 0){
                /** @var DiscountSalesRule $rule */
                foreach ($freeRules as $rule){
                    if (!is_null($rule->start_time) && !is_null($rule->end_time)){
                        if (strtotime($rule->start_time) <= time() && time() < strtotime($rule->end_time)){
                            if ($total_amount >= $rule->satisfy_amount){
                                return $rule->name;
                            }
                        }
                    } else {
                        if ($total_amount >= $rule->satisfy_amount){
                            return $rule->name;
                        }
                    }
                }
            }
        }
        return '';
    }
    public static function getGoodsReduceId($merchant_medicine_id,$total_amount=0.01){
        $goods_rules=self::whereMerchantMedicineId($merchant_medicine_id)->pluck('discount_sales_rule_id')->toArray();
        if (count($goods_rules) > 0){
            $freeRules=DiscountSalesRule::whereType('reduce')->whereIn('id',$goods_rules)->whereStatus(1)->orderBy('satisfy_amount','desc')->get();
            if (count($freeRules) > 0){
                /** @var DiscountSalesRule $rule */
                foreach ($freeRules as $rule){
                    if (!is_null($rule->start_time) && !is_null($rule->end_time)){
                        if (strtotime($rule->start_time) <= time() && time() < strtotime($rule->end_time)){
                            if ($total_amount >= $rule->satisfy_amount){
                                return $rule->id;
                            }
                        }
                    } else {
                        if ($total_amount >= $rule->satisfy_amount){
                            return $rule->id;
                        }
                    }
                }
            }
        }
        return '';
    }
    public static function getGoodsRuleIds($merchant_medicine_id){
        $goods_rules=self::whereMerchantMedicineId($merchant_medicine_id)->pluck('discount_sales_rule_id')->toArray();
        $rule_array=[];
        /*
         *$rule_array= [1=>100,2=>200,3=>4399];
         */
        if (count($goods_rules) > 0){
            $freeRules=DiscountSalesRule::whereType('reduce')->whereIn('id',$goods_rules)->whereStatus(1)->orderBy('satisfy_amount','desc')->get();
            if (count($freeRules) > 0){
                /** @var DiscountSalesRule $rule */
                foreach ($freeRules as $rule){
                    if (!is_null($rule->start_time) && !is_null($rule->end_time)){
                        if (strtotime($rule->start_time) <= time() && time() < strtotime($rule->end_time)){
                            $rule_array[$rule->id]=$rule->satisfy_amount;
                        }
                    } else {
                        $rule_array[$rule->id]=$rule->satisfy_amount;
                    }
                }
            }
        }
        return $rule_array;
    }
}
